// pages/manage/guidance/guidanceDetail/guidanceDetail.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskInfo: null,
    userInfo: null,
    userAge: '无记录',
    array: null,
    arrowUrl: "/image/13.png",
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '诊后指导', //导航栏 中间的标题
    },
    navHeight: app.globalData.statusBarHeight * 2 + 86,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon:'none'
    })
    setTimeout(function(){
      wx.request({
        url: app.getUrl('/xcx/guidance/queryGuidanceDetail'),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        data: {
          taskId: options.taskId,
        },
        success(res) {
          if (res.data.code == 0) {
            for (var i in res.data.data.dataContent) {
              var innerItem = res.data.data.dataContent[i];
              for (var j in innerItem) {
                innerItem[j].flag = false;
              }
            }
            that.setData({
              taskInfo: res.data.data.taskInfo,
              array: res.data.data.dataContent
            })
            getCurrentUserInfo();
          } else if (res.data.code == 404) {

          }
          wx.hideLoading();
        }
      })
    },1000)
      var getCurrentUserInfo = function (){
      let {openid,accountId} = wx.getStorageSync('logindata')
      wx.request({
        // url: app.getUrl('/xcx/pt/getCurrentUserInfo'),
        url: app.getUrl(`/xcx/pt/getCurrentUserInfo?openId=${openid}&accountId=${accountId}`),
        method: 'GET',
        header: wx.getStorageSync('cookie'),
        success(res) {
          if (res.data.code == 0) {
            that.calcuAge(res.data.data.birthday, that.data.taskInfo.finishDate);
            that.setData({
              userInfo: res.data.data,
              // userAge: age
            })
          }
        }
      })
    }
  },
  // 年龄计算
  calcuAge: function(birthDay,finishDate) {
     var nowDate = new Date(finishDate);
     var birthDayDate = new Date(birthDay);
     var age="";
     var day,week,month,year
     var days = parseInt((nowDate.getTime()-birthDayDate.getTime())/1000/3600/24)
     if(days<3*30){
       week=parseInt(days/7)
       day=parseInt(days%7)
       week ? age = week + "周" + (day ? (day + "天") : '') : age = day + "天";
     }else if(days<365){
       month=parseInt(days/30)
       day = parseInt(days%30)
       age = month + "月龄" + (day ? day + "天" : "");
     }else if(days<6*365){
       year=parseInt(days/365)
       month=parseInt((days%365)/30.4)
       age = year + "岁" + (month ? month + "月龄" : "") 
     }else{
       year=parseInt(days/365)
       age = year + "岁"
     }
    this.setData({
      userAge: age
    })
    // var nowDate = new Date(finishDate);
    // var birthDayDate = new Date(birthDay);
    // var age="";
    // var day = nowDate.getDate() - birthDayDate.getDate()
    // var month = nowDate.getMonth() - birthDayDate.getMonth()
    // var year = nowDate.getFullYear() - birthDayDate.getFullYear()
    // if (day < 0) {
    //   month = month - 1
    //   //判断月份天数
    //   var nowMonth = nowDate.getMonth() - 1
    //   if (nowMonth == 1 || nowMonth == 3 || nowMonth == 5 || nowMonth == 7 || nowMonth == 8 || nowMonth == 10 || nowMonth == 12) {
    //     day = nowDate.getDate() - birthDayDate.getDate() + 31
    //   } else if (nowMonth == 4 || nowMonth == 6 || nowMonth || nowMonth == 9 || nowMonth == 11) {
    //     day = nowDate.getDate() - birthDayDate.getDate() + 30
    //   } else {
    //     if ((nowDate.getFullYear() % 4 == 0 && nowDate.getFullYear() % 100 != 0) || (nowDate.getFullYear() % 400 == 0)) {
    //       day = nowDate.getDate() - birthDayDate.getDate() + 29
    //     } else {
    //       day = nowDate.getDate() - birthDayDate.getDate() + 28
    //     }
    //   }
    // }
    // if (month < 0) {
    //   month = month + 12
    //   year = year - 1
    // }
    // if (year > 0) {
    //   age = year + "岁"
    // }
    // if (month > 0) {
    //   age = age + month + "月"
    // }
    // if (day >= 0) {
    //   age = age + day + "天"
    // }
    // this.setData({
    //   userAge: age
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  spread: function(e) {
    var innerkey = e.currentTarget.dataset.innerkey,
      key = e.currentTarget.dataset.key;
    var dataArray = this.data.array;
    dataArray[key][innerkey].flag = !dataArray[key][innerkey].flag;
    this.setData({
      array: dataArray
    })
  }
})